import json

import requests
from urllib.parse import unquote, quote

with open('cookie.json','r',encoding='utf-8') as f:
    headers = json.load(f)
def query(reciveNum):
    global courseId, bsNum
    params = {
        'method': 'queryBusinessInfo',
    }
    data = {"reciveNum":reciveNum}
    response = requests.post(
        'http://59.215.206.188/tyspzy/approval/business/error',
        params=params,
        headers=headers,
        json=data,
        verify=False,
    ).json()

    lists=[]
    #当前环节名
    # nodeName=response['flowTask'][0]['nodeName']
    flowTaskId=response['flowTask'][0]['taskId']
    CANDIDATEID=response['flowTask'][0]['CANDIDATEID']
    nodeName=response['flowTask'][0]['nodeName']
    nodeId=response['flowTask'][0]['nodeId']
    for i in response['courseList']:
        if i['active']=='1':
            courseId=i['id']
            bsNum=i['bsNum']
    lists.append([flowTaskId,courseId,bsNum,CANDIDATEID,nodeId,nodeName])

    return lists
def 补齐流程(lists):
    params = {
        'method': 'operateFlow',
    }

    data = {"courseId":lists[0][1],
            "flowTaskId":lists[0][0],
            "bsNum":lists[0][2],
            }

    response = requests.post(
        'http://59.215.206.188/tyspzy/approval/business/error',
        params=params,

        headers=headers,
        json=data,
        verify=False,
    )
    print(response.text)
    return response.text

def 补齐业务(list):
    params = {
        'method': 'operateCourse',
    }
    #lists.append([flowTaskId,courseId,bsNum,CANDIDATEID,nodeId,nodeName])
    data = {"courseId":list[0][1],
            "flowTaskId":list[0][0],
            "taskCanda":quote(quote(list[0][3])),
            "nodeId":list[0][4],
            "nodeName":quote(quote(list[0][5])),
            "bsNum":list[0][2]}

    response = requests.post(
        'http://59.215.206.188/tyspzy/approval/business/error',
        params=params,

        headers=headers,
        json=data,
        verify=False,
    )
    print(response.text)
    return response.text


def start(reciveNum):
    querys=query(reciveNum)
    补齐流程结果=补齐流程(querys)
    querys = query(reciveNum)
    补齐业务结果 = 补齐业务(querys)
    print(补齐流程结果)
    print(补齐业务结果)
    return 补齐流程结果+补齐业务结果

